package caip2021p.rcv2;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        String[] lineArr = sc.nextLine().split(" ");
        String input = lineArr[0];
        int base = Integer.parseInt(lineArr[1]);
        
        if (base == 10) {
            // 十进制转27进制
            long num = 0;
            long k = 1;
            // 将字符串转换为长整型
            for (int i = input.length() - 1; i >= 0; --i) {
                num += k * (input.charAt(i) - '0');
                k *= 10;
            }
            
            if (num == 0) {
                System.out.println("0");
                return;
            }
            
            StringBuilder s = new StringBuilder();
            while (num != 0) {
                long remainder = num % 27;
                if (remainder < 10) {
                    s.insert(0, (char)('0' + remainder));
                } else {
                	//ASCII码转字符
                    s.insert(0, (char)('A' + remainder - 10));
                }
                num /= 27;
            }
            System.out.println(s.toString());
        } else {
            // 27进制转十进制
            long num = 0;
            long k = 1;
            for (int i = input.length() - 1; i >= 0; --i) {
                char c = input.charAt(i);
                if (c <= '9') {
                    num += k * (c - '0');
                } else {
                    num += k * (c - 'A' + 10);
                }
                k *= 27;
            }
            System.out.println(num);
        }
    }
}